<?php
namespace Base;

	class Request
	{
		protected $debug  = false;		
		protected $data;
		protected $action = false;
		public function __construct(&$data,$action) 
		{
			$this->data = $data;
			$this->action=$action;
		}
		protected function success($message,$gotoAction=false,$waitSecond=3,$ajax=false)
		{
			if ($gotoAction){
				$this->assign('waitSecond',$waitSecond);
				$this->assign('jumpUrl',$gotoAction);
			}
			$this->action->dispatchJump($message,1,$ajax);
		}
		protected function error($message,$gotoAction=false,$waitSecond=3,$ajax=false)
		{
			if ($gotoAction){
				$this->assign('waitSecond',$waitSecond);
				$this->assign('jumpUrl',$gotoAction);
			}
			$this->action->dispatchJump($message,0,$ajax);
		}		
		public function __set($name, $value) 
		{
			$this->data[$name] = $value;
		}

		public function __get($name) 
		{
			$method='strip_tags';
			switch (strtolower(substr($name,-2))) {
				case '_i':
				    $method='intval';
					$name=substr($name,0,-2);
					break;
				case '_f':
				    $method='floatval';
					$name=substr($name,0,-2);
					break;	
			}
			if (array_key_exists($name, $this->data)) {
				return $method($this->data[$name]);
			}else{
				return null;
			}
		}
		public function get_array($fields){
			if (is_string($fields)){
				$fields=explode(',',$fields);
			}
			$filter=C('DEFAULT_FILTER');
			foreach($fields as $field){
				$require=false;$name='';$re_check=false;$re_tip=false;
				if (is_array($field)){
					$name=$field[0];
					if (isset($field[1])) $require=$field[1];
					if (isset($field[2])) $re_check=$field[2];
					if (isset($field[3])) $re_tip=$field[3];
				}else{
					$name=$field;
				}
				if ( isset($this->data[$name]) && $this->data[$name]!='' ){
					$data[$name]=$filter($this->data[$name]);
					if (is_string($re_check) && $re_check !== '' && !preg_match($re_check, $data[$name])) {
						$this->error($re_tip);
						return false;
					}
				}else if ($require){
					$this->error($require);
					return false;
				}
			}
			return $data;
		}		
		public function __isset($name) 
		{
			return isset($this->data[$name]);
		}

		public function __unset($name) 
		{
			unset($this->data[$name]);
		}
		public function __invoke($name,$method='strip_tags',$default=null){
			if (array_key_exists($name, $this->data)){
				switch ($method) {
					case '%i':	case 'i': case 'int':
						$method='intval';
						break;
					case '%f': case 'f': case 'float':						
						$method='floatval';
						break;	
				}			
				return $method($this->data[$name]);
			}else{
				return $default;
			}		
		}
		//this->get->intval('test',0);	
		public function __call($name,$args){
			if (count($args)<=0){
				if (array_key_exists($name, $this->data)) {
					return strip_tags($this->data[$name]);
				}else{
					return null;
				}			
			}else{
				if (array_key_exists($args[0], $this->data)&&$this->data[$args[0]]) {
					return $name($this->data[$args[0]]);
				}else{
					return isset($args[1]) ? $args[1] : NULL;
				}				
			}
		}
	}
?>